既然完成了第一頁的大部分的要素,那應該就要來建構其他的功能
為了達成這個要素,TabbarView會是我們會用到的元件
在建立tabBarController之前先建立enum來儲存必要的要素
enum tabType {
case first
case second
case third
case four
var img: String {
switch self {
case .first:
return "personalhotspot"
case .second:
return "play.square"
case .third:
return "giftcard.fill"
case .four:
return "clock.circle.fill"
}
}
var title: String {
switch self {
case .first:
return "First"
case .second:
return "Second"
case .third:
return "Third"
case .four:
return "Four"
}
}
var vc: Any
}
class tabBarController: UITabBarController {
let subVC:[tabType] = [.first, .second, .third, .four]
override func viewDidLoad() {
configure()
}
func configure() {
for i in subVC {
let vc = UIViewController()
vc.view.backgroundColor = .white
vc.tabBarItem.image = UIImage(systemName: "\(i.img)")
vc.tabBarItem.title = i.title
// 必須要將VC加入到tabBarController內
addChild(vc)
}
}
}
因為tabBarView要成為第一個出現的ViewController
所以要指定為rootViewController
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let _ = (scene as? UIWindowScene) else { return }
guard let windowSence = (scene as? UIWindowScene) else { return }
window = UIWindow(frame: windowSence.coordinateSpace.bounds)
window?.windowScene = windowSence
window?.rootViewController = tabBarController()
window?.makeKeyAndVisible()
}
以上就可以簡單創造出TabViewController
坑:
記得要把那些創造出來的VC加入進去childVC,否則那些View是不會顯現